﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HCFramework.Product.FrontEndControl
{
    public partial class ListRelatedProduct : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            if (M.UrlQuery.productID.HasValue)
            {
                API.LINQ.Product product = C.ProductController.GetByID(M.UrlQuery.productID.Value);
                ListView1.DataSource = product.ProductCategory.Products
                    .Where(ik => ik.ProductID != product.ProductID && ik.IsActive)
                    .OrderByDescending(ik => ik.DateTimeInsert)
                    .Take(10)
                    .ToList();
                ListView1.DataBind();
            }
        }

        protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                int productId = (int)DataBinder.Eval(e.Item.DataItem, "ProductID");
                API.LINQ.Product product = C.ProductController.GetByID(productId);
                string imageUrl = C.ProductImageController.GetMainImageURL(product);
                Image Image1 = e.Item.FindControl("Image1") as Image;
                Image1.ImageUrl = imageUrl;
            }
        }
    }
}